package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbUtil;
    @Override
    public int insert(User user)  {
//        System.out.println("UserMApper:向user表中插入用户数据");
        try(Connection connection=dbUtil.getConnection()){
            String sql="insert into user(username,password,nickname,age) values(?,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }
        return 0;
//        System.out.println("UserMapper:执行Insert操作");
//        System.out.println("UserMapper:执行完毕，返回影响的条目数");
//        return 0;

    }
    @Override
    public User selectByUsername(String username){
        try(Connection connection=dbUtil.getConnection()){
            String sql="SELECT id,username,password,nickname,age  " +
                        "FROM user  "+
                        "WHERE username=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                User user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public List<User> selectAll(){
        List<User> userlist=new ArrayList<>();
        try(Connection connection=dbUtil.getConnection()){
            String sql="SELECT id,username,password,nickname,age  " +
                    "FROM user";
            Statement statement=connection.createStatement();
            ResultSet rs=statement.executeQuery(sql);
            while(rs.next()){
                User user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                userlist.add(user);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return userlist;
    }
    @Override
    public int update(User user){
        try(Connection connection=dbUtil.getConnection()){
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username=scanner.nextLine();
            String sql="UPDATE user SET password=?,nickname=?,age=? WHERE username=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,user.getPassword());
            statement.setString(2,user.getNickname());
            statement.setInt(3,user.getAge());
            statement.setString(4,username);
            int num=statement.executeUpdate(sql);
            System.out.println(num>0?"修改密码成功！":"修改密码失败！");
            System.out.println(num+"行数据受影响！");
        }catch (SQLException e){
            e.printStackTrace();
        }



        return 0;
    }
}
